<template>
  <div class="user">
    <div class="user-info">
      <div class="avatar">
        <img :src="userInfo.avatar" alt="">
      </div>
      <div class="username">{{ userInfo.name }}</div>
    </div>
    <van-grid :column-num="3">
      <van-grid-item to="/" icon="clock-o" text="历史记录" />
      <van-grid-item to="/layout/collect" icon="bookmark-o" text="我的收藏" />
      <van-grid-item to="/layout/like" icon="thumb-circle-o" text="我的点赞" />
    </van-grid>

    <van-cell-group>
      <van-cell title="推荐分享" is-link />
      <van-cell title="意见反馈" is-link />
      <van-cell title="关于我们" is-link />
      <van-cell title="退出登录" is-link @click="exit" />
    </van-cell-group>
  </div>
</template>

<script>
import { getUserInfoApi } from '@/api/user';
import { Toast } from 'vant';

export default {
  name: 'TtUser',
  data() {
    return {
      userInfo: {
        avatar: '',
        name: ''
      }
    }
  },
  methods: {
    async getUserInfo() {
      const res = await getUserInfoApi()
      // console.log(res)
      this.userInfo.avatar = res.data.avatar
      this.userInfo.name = res.data.username
    },
    exit() {
      // 清除token,以及替换跳转登录页
      localStorage.removeItem('token')
      this.$router.replace('/login')
      Toast.success('退出成功')
    }
  },
  created() {
    this.getUserInfo()
  }
}
</script>

<style lang="less" scoped>
  .user {
    height: 100%;
    padding: 0 8px;
    background-color: #f5f5f5;

    .user-info {
      // height: 100px;
      // background-color: #fff;
      display: flex;
      align-items: center;
      padding: 20px 12px;

      .avatar {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        overflow: hidden;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .username {
        flex: 1;
        margin-left: 20px;
        font-size: 20px;
        font-weight: 700;
      }
    }

    .van-grid {
      margin-top: 20px;
    }

    .van-cell-group {
      margin-top: 20px;
    }
  }
</style>